package com.weayer.controller;

import com.weayer.dto.ResponseResult;
import com.weayer.response.NumberCodeResponse;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: Service服务 - 鉴权 - 随机数controller
 * @author: Weayer
 **/
@RestController
public class NumberCodeController {
    @GetMapping("/numberCode/{size}")   //生成随机数，数的位数为size
    public ResponseResult numberCode(@PathVariable("size") int size){
//        System.out.println("size: " + size);
        // (随机数 1 ~ 10) * 10 ^ (size-1)
        double mathRandom = (Math.random()* 9 + 1) * (Math.pow(10,size-1));
//        System.out.println(mathRandom);
        int resultRandom = (int) mathRandom;
//        System.out.println(resultRandom);

        NumberCodeResponse response = new NumberCodeResponse();
        response.setNumberCode(resultRandom);

        return ResponseResult.success(response);
    }

    public static void main(String[] args) {
        double mathRandom = (Math.random()* 9 + 1) * (Math.pow(10,5));
        System.out.println(mathRandom);
        int resultRandom = (int) mathRandom;
        System.out.println(resultRandom);
    }
}
